# Fixup bundle script generated at configure time. It expects that the
# following variables are set:
#  BUILD_GUI
#  MTS_LIBPATHS (the result of calling mts_library_paths)
#  MTS_BUNDLE_NAME
#  MTS_EXE_DEST
#  MTS_LIB_DEST
#  MTS_PLUGIN_DEST
#  MTS_PYTHON_DEST

message (STATUS "Running Mitsuba Fixup Bundle script...")

# Copy the stand alone dynamic libraries into @executable_path/../Frameworks
# instead of the default @executable_path/../MacOS.
if (APPLE)
  # gp_item_default_embedded_path item default_embedded_path_var
  #
  # Return the path that others should refer to the item by when the item
  # is embedded inside a bundle.
  #
  # Override on a per-project basis by providing a project-specific
  # gp_item_default_embedded_path_override function
  #
  function (gp_item_default_embedded_path_override item default_embedded_path_var)
    if(item MATCHES "\\.dylib$" AND ${${default_embedded_path_var}} STREQUAL "@executable_path/../MacOS")
        set(path "@executable_path/../Frameworks")
        set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
    endif()
  endfunction ()
  
  # GP_RESOLVE_ITEM(<context> <item> <exepath> <dirs> <resolved_item_var> <resolved_var>)
  #
  # Resolve an item into an existing full path file.
  # Override on a per-project basis by providing a project-specific
  # gp_resolve_item_override function.
  #
  function (gp_resolve_item_override context item exepath dirs resolved_item_var resolved_var)
    set (dependency_dest "${CMAKE_INSTALL_PREFIX}/@MTS_LIB_DEST@")
    if (item MATCHES "^libmitsuba.+\\.dylib$")
      # Since the bundle script runs after installation, the mitsuba libraries
      # are already in their destination location
      set (ri "${dependency_dest}/${item}")
      set (${resolved_item_var} "${ri}" PARENT_SCOPE)
      set (${resolved_var} 1 PARENT_SCOPE)
    elseif (item MATCHES "^@rpath/.+")
      # The first time CMake call this function, the dependencies have not been
      # copied. In the first pass they will be resolved to their external
      # locations so that they may be copied into the bundle. In the fixup pass
      # they will be resolved to their embedded locations
      foreach(depdir "${dependency_dest}" ${dirs})
        string (REGEX REPLACE "^@rpath/(.+)" "\@depdir\@/\\1" ri_temp "${item}")
        string (CONFIGURE "${ri_temp}" ri @ONLY)
	if(EXISTS "${ri}")
          set (${resolved_item_var} "${ri}" PARENT_SCOPE)
          set (${resolved_var} 1 PARENT_SCOPE)
	  return()
	endif()
      endforeach()
    elseif (item MATCHES "^@(executable|loader)_path/\\.\\./[Ff]rameworks/.+")
      # The same applies for the frameworks
      foreach(depdir "${dependency_dest}" ${dirs})
        string (REGEX REPLACE "^@(executable|loader)_path/\\.\\./[Ff]rameworks/(.+)" "\@depdir\@/\\2" ri_temp "${item}")
        string (CONFIGURE "${ri_temp}" ri @ONLY)
	if(EXISTS "${ri}")
          set (${resolved_item_var} "${ri}" PARENT_SCOPE)
          set (${resolved_var} 1 PARENT_SCOPE)
	  return()
	endif()
      endforeach()
    endif ()
  endfunction ()
  
  # As of CMake 2.8.5 there seems to be a bug: the override version only takes
  # two arguments: the resolved filename and type_var
  function (gp_resolved_file_type_override file type_var)
    if ("${${type_var}}" STREQUAL "other")
      set (base_preffix "${CMAKE_INSTALL_PREFIX}/@MTS_LIB_DEST@/")
      string (LENGTH "${base_preffix}" min_embedded_len)
      string (LENGTH "${file}" file_len)
      if (file_len GREATER min_embedded_len)
        string(SUBSTRING "${file}" 0 ${min_embedded_len} file_substr)
        if (file_substr STREQUAL base_preffix)
          set (${type_var} "embedded" PARENT_SCOPE)
        endif ()
      endif ()
    endif()
  endfunction ()

elseif (WIN32)

  # Resolve the python library as a system component
  function (gp_resolve_item_override context item exepath dirs resolved_item_var resolved_var)
    if (item MATCHES "^python[23][0-9]\\.dll$")
      set (ri "C:/Windows/System32/${item}")
      set (${resolved_item_var} "${ri}" PARENT_SCOPE)
      set (${resolved_var} 1 PARENT_SCOPE)
    endif ()
  endfunction ()
endif ()


# Get all the installed mitsuba and Qt plugins
file (GLOB MTSPLUGINS
  "${CMAKE_INSTALL_PREFIX}/@MTS_PLUGIN_DEST@/*${CMAKE_SHARED_LIBRARY_SUFFIX}")
file (GLOB_RECURSE QTPLUGINS
  "${CMAKE_INSTALL_PREFIX}/@MTS_QT_PLUGIN_DEST@/*${CMAKE_SHARED_LIBRARY_SUFFIX}")
  
# Get the python bindings as well
file (GLOB PYBINDINGS "${CMAKE_INSTALL_PREFIX}/@MTS_PYTHON_DEST@/*")

# Get an executable or just the bundle name for fixup
if (NOT DEFINED BUILD_GUI)
  set (BUILD_GUI @BUILD_GUI@)
endif ()
if (NOT APPLE OR NOT BUILD_GUI)
  get_filename_component(EXEDIR "${CMAKE_INSTALL_PREFIX}/@MTS_EXE_DEST@" ABSOLUTE)
  file (GLOB mts_exes "${EXEDIR}/mts*${CMAKE_EXECUTABLE_SUFFIX}")
  list (GET mts_exes 0 APP)
else ()
  get_filename_component(APP "${CMAKE_INSTALL_PREFIX}/@MTS_BUNDLE_NAME@" ABSOLUTE)
endif ()

set (libpaths "@MTS_LIBPATHS@")


# Use our custom version of fixup_bundle, which supports @rpath on OS X
list(APPEND CMAKE_MODULE_PATH "@CMAKE_MODULE_PATH@")
include("MtsBundleUtilities")
if(APPLE)
  set(BU_CHMOD_BUNDLE_ITEMS ON)
endif()
mts_fixup_bundle("${APP}" "${MTSPLUGINS};${QTPLUGINS};${PYBINDINGS}" "${libpaths}")

